package com.elink.enginerring.library;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.common.Constant;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.library.co.ThesisCo;
import com.elink.enginerring.library.entity.Thesis;
import com.elink.enginerring.library.service.IThesisService;
import com.elink.enginerring.user.entity.User;

@Controller
@RequestMapping("view/thesis")
@Api(value = "论文图书馆接口")
@ResponseBody
public class ThesisController {
	@Autowired
	private IThesisService thesisService;

	/**
	 * 获取论文列表
	 * @param co
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@ApiOperation(value = "获取论文列表", httpMethod = "GET", notes = "根据条件分页获取论文")
	public Map<String, Object> getList(@ModelAttribute @Valid ThesisCo co) {
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return thesisService.getListOfPage(co);
	}

	/**
	 * 获取论文详情
	 * @param thesisId
	 * @return
	 */
	@RequestMapping(value = "/getInfo", method = RequestMethod.GET)
	@ApiOperation(value = "获取论文详情", httpMethod = "GET", notes = "获取论文详情")
	public Map<String, Object> getInfo(@ApiParam(value = "论文id，比如'1'", required = true) 
	@RequestParam(value = "thesisId", required = true) String thesisId) {
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return thesisService.getInfo(thesisId);
	}

	/**
	 * 添加论文
	 * @param thesis
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "添加论文", httpMethod = "POST", notes = "添加论文")
	public Map<String, Object> add(@ModelAttribute @Valid Thesis thesis,
			HttpServletRequest request) {
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		HttpSession session = request.getSession();
		User user=(User) session.getAttribute(Constant.su_in_session);
		thesis.setUserId(user.getUserId());
		return thesisService.add(thesis);
	}

	/**
	 * 论文修改
	 * @param thesis
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ApiOperation(value = "修改论文", httpMethod = "POST", notes = "修改论文")
	public Map<String, Object> update(@ModelAttribute @Valid Thesis thesis) {
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return thesisService.update(thesis);
	}

	/**
	 * 删除论文
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	@ApiOperation(value = "删除论文", httpMethod = "GET", notes = "删除论文")
	public Map<String, Object> del(@ApiParam(value = "论文ids，比如'1,.2,3'", required = true) 
	@RequestParam(value = "ids", required = true) String ids) {
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return thesisService.delete(ids);
	}
	
	/**
	 * 论文认证
	 * @param ids
	 * @param authStatus 1--通过认证 2--不通过认证
	 * @return
	 */
	@RequestMapping(value = "/auth", method = RequestMethod.GET)
	@ApiOperation(value = "认证论文", httpMethod = "GET", notes = "认证论文")
	public Map<String, Object> auth(@ApiParam(value = "论文ids，比如'1,.2,3'", required = true) 
	@RequestParam(value = "ids", required = true) String ids,
	@ApiParam(value = "认证状态'", required = true) 
	@RequestParam(value = "authStatus", required = true) int authStatus) {
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return thesisService.auth(ids,authStatus);
	}
}
